import Vue from 'vue';
import VueRouter from 'vue-router';

// Token 工具类
const TokenUtil = {
    getToken() {
        return localStorage.getItem('token');
    },
    setToken(token) {
        localStorage.setItem('token', token);
    },
    removeToken() {
        localStorage.removeItem('token');
    },
    hasToken() {
        return !!this.getToken();
    }
};
//注册路由组件
Vue.use(VueRouter);
//编写路由规则
const routes = [
     {
         path: '/',
         component: () => import('@/view/home/HomePage')
     },
     {
         path: '/login',
         name: 'LoginPage',
         component: () => import('@/view/home/LoginPage')
     },

]
//创建路由
const router = new VueRouter({
    mode:'hash',
    routes
});
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        next();
    } else {
        if (TokenUtil.hasToken()) {
            next();
        } else {
            next('/login');
        }
    }
});
//导出
export default router;